<template>
    <div>
        <h1>编辑</h1>
        <hr>
        <label>
            <span>userName:</span>
            <input type="text" v-model="userName">
        </label>
        <br>
        <label>
            <span>userAge:</span>
            <input type="number" v-model="userAge">
        </label>
        <br>
        <label>
            <span>userAddress:</span>
            <input type="text" v-model="userAddress">
        </label>
        <br>
        <button @click="editData">确定</button>
    </div>
</template>

<script>
import axios from '@/service/httpService'
export default {
  data () {
    return {
      id: '',
      userName: '',
      userAge: '',
      userAddress: ''
    }
  },
  methods: {
    getData () {
      axios
        .get('/selectUserById', {
          params: {
            id: this.$route.query.id
          }
        })
        .then(response => {
          this.id = response.data.id
          this.userName = response.data.userName
          this.userAge = response.data.userAge
          this.userAddress = response.data.userAddress
          console.log(response)
        })
        .catch((error) => {
          console.log(error)
        })
    },
    editData () {
      axios({
        method: 'put',
        url: '/updateUser',
        data: {id: this.id, userName: this.userName, userAge: this.userAge, userAddress: this.userAddress}
      })
        .then(response => {
          console.log(response)
          this.$router.push({path: '/'})
        }).catch(error => {
          console.log(error)
        })
    }
  },
  created () {
    this.getData()
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
